<?php
declare(strict_types=1);

namespace app\task;

use app\common\model\card\CardLimitRetry;
use payment\SlashService;
use think\facade\Log;

class CardLimitRetryTask
{
    /**
     * 处理限额重试任务
     * @return void
     */
    public function handle(): void
    {
        try {
            // 获取待处理的重试记录
            $records = CardLimitRetry::getPendingRecords(10);
            
            foreach ($records as $record) {
                try {
                    // 根据卡bin类型处理不同的支付服务
                    if ($record['card_bin'] == 12) { // Slash卡片
                        $slash = new SlashService(12);
                        $limits = [];
                        
                        // 设置单笔限额
                        if ($record['single_transaction_limit'] > 0) {
                            $limits['single'] = [
                                'amount_cents' => $record['single_transaction_limit'] * 100,
                                'currency' => 'USD'
                            ];
                        }
                        
                        // 设置每日限额
                        if ($record['daily_transaction_limit'] > 0) {
                            $limits['daily'] = [
                                'amount_cents' => $record['daily_transaction_limit'] * 100,
                                'currency' => 'USD'
                            ];
                        }
                        
                        // 设置周期限额
                        if ($record['weekly_transaction_limit'] > 0) {
                            $limits['weekly'] = [
                                'amount_cents' => $record['weekly_transaction_limit'] * 100,
                                'currency' => 'USD'
                            ];
                        }
                        
                        // 设置每月限额
                        if ($record['monthly_transaction_limit'] > 0) {
                            $limits['monthly'] = [
                                'amount_cents' => $record['monthly_transaction_limit'] * 100,
                                'currency' => 'USD'
                            ];
                        }
                        
                        // 设置年度限额
                        if ($record['yearly_transaction_limit'] > 0) {
                            $limits['yearly'] = [
                                'amount_cents' => $record['yearly_transaction_limit'] * 100,
                                'currency' => 'USD'
                            ];
                        }
                        
                        // 设置总限额
                        if ($record['total_transaction_limit'] > 0) {
                            $limits['total'] = [
                                'amount_cents' => $record['total_transaction_limit'] * 100,
                                'currency' => 'USD'
                            ];
                        }
                        
                        // 调用API设置限额
                        if (!empty($limits)) {
                            $result = $slash->setMultipleCardLimits($record['card_id'], $limits);
                            if ($result) {
                                // 更新状态为成功
                                CardLimitRetry::updateRetryStatus($record['card_id'], CardLimitRetry::STATUS_SUCCESS);
                            } else {
                                // 更新状态为失败
                                CardLimitRetry::updateRetryStatus(
                                    $record['card_id'],
                                    CardLimitRetry::STATUS_FAILED,
                                    'Slash卡片限额设置失败'
                                );
                            }
                        }
                    }
                } catch (\Exception $e) {
                    // 记录错误日志
                    Log::error('Card limit retry failed: ' . $e->getMessage(), [
                        'card_id' => $record['card_id'],
                        'error' => $e->getMessage()
                    ]);
                    
                    // 更新状态为失败
                    CardLimitRetry::updateRetryStatus(
                        $record['card_id'],
                        CardLimitRetry::STATUS_FAILED,
                        $e->getMessage()
                    );
                }
            }
        } catch (\Exception $e) {
            Log::error('Card limit retry task failed: ' . $e->getMessage());
        }
    }
} 